﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace LogBase
{
    public class Filter
    {
        /// <summary>
        /// 获取所有站点页面过滤列表
        /// </summary>
        /// <returns>DataTable</returns>
        public static DataTable getAll()
        {
            string sql = "select [sitename],[url],[comment],[Enable],(case [Enable] when 1 then '启用' else '禁用' end) status from [QT_URLExclude] order by [sitename],[url] asc";
            DataTable dt = new QT.DBO.SqlDBHelper(MyDB.ConnString).DataTable(sql);
            return dt;
        }

        /// <summary>
        /// 获取指定站点的页面过滤列表
        /// </summary>
        /// <param name="sitename">站点ID</param>
        /// <returns>DataTable</returns>
        public static DataTable getAll(string sitename)
        {
            string sql = "select [guid],[url],[comment],[Enable],(case [Enable] when 1 then '启用' else '禁用' end) status from [QT_URLExclude] where [sitename]='" + sitename + "' order by [url] asc";
            DataTable dt = new QT.DBO.SqlDBHelper(MyDB.ConnString).DataTable(sql);
            return dt;
        }

        /// <summary>
        /// 新增页面过滤配置
        /// </summary>
        /// <param name="sitename">站点ID</param>
        /// <param name="url">URL地址</param>
        /// <param name="comment">备注</param>
        /// <param name="enable">状态ID</param>
        /// <returns>int</returns>
        public static int Add(string sitename,string url,string comment,string enable)
        {
            if (Exsits(null, sitename, url)) return -2;

            string sql = "insert into [QT_URLExclude]([guid],[sitename],[url],[comment],[enable]) values(replace(newid(),'-',''),'" + sitename + "','" + url + "','" + comment + "'," + enable + ")";
            int rval = new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql);
            return rval;
        }

        /// <summary>
        /// 修改页面过滤配置
        /// </summary>
        /// <param name="guid">GUID</param>
        /// <param name="sitename">站点ID</param>
        /// <param name="url">URL地址</param>
        /// <param name="comment">备注</param>
        /// <param name="enable">状态ID</param>
        /// <returns>int</returns>
        public static int Update(string guid,string sitename, string url, string comment,string enable)
        {
            if (Exsits(guid, sitename, url)) return -2;

            string sql = "update [QT_URLExclude] set [url]='" + url + "',[comment]='" + comment + "',[enable]=" + enable + " where [guid]='" + guid + "' and [sitename]='" + sitename + "'";
            int rval = new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql);
            return rval;
        }

        /// <summary>
        /// 更新页面过滤配置的状态
        /// </summary>
        /// <param name="guid">GUID</param>
        /// <param name="sitename">站点ID</param>
        /// <param name="enable">状态ID</param>
        /// <returns></returns>
        public static int UpdateStatus(string guid, string sitename, string enable)
        {
            string sql = "update [QT_URLExclude] set [Enable]=" + enable + " where [guid]='" + guid + "' and [sitename]='" + sitename + "'";
            int rval = new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql);
            return rval;
        }

        /// <summary>
        /// 删除页面过滤配置
        /// </summary>
        /// <param name="guids">一组记录ID</param>
        /// <returns>int</returns>
        public static int Delete(List<string> guids)
        {
            string sql = "delete from [QT_URLExclude] where [guid] in('" + string.Join("','", guids.ToArray()) + "')";
            int rval = new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql);
            return rval;
        }

        /// <summary>
        /// 检查过滤地址是否存在
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool Exsits(string guid, string sitename, string url)
        {
            string sql = "select [guid] from [QT_URLExclude] where [sitename]='" + sitename + "' and [url]='" + url + "'" + (string.IsNullOrEmpty(guid) ? "" : (" and [guid]<>'" + guid + "'"));
            DataTable dt = new QT.DBO.SqlDBHelper(MyDB.ConnString).DataTable(sql);
            if (dt.Rows.Count == 0) return false;
            else return true;
        }
    }
}
